package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbMessage;
import com.woniuxy.entitys.TbSendmessage;
import com.woniuxy.services.MessageService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 分页查询，获得所有的消息
     * @return
     */
    @PostMapping("/message/list")
    public ResultVO getMessageList(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<TbMessage> pageInfo = messageService.getMessageList(pageNum, pageSize);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("failed");
    }

    /**
     * 给消息表增加消息
     */
    @PostMapping("/message/save")
    public ResultVO saveMessage(@RequestBody Map<String,Object> map){
        /**
         *  title: this.messageAddForm.title,
         *         content: this.messageAddForm.content,
         */
        String title = (String) map.get("title");
        String content = (String) map.get("content");
        TbMessage tbMessage = new TbMessage();
        tbMessage.setSoftdel(1);
        tbMessage.setTitle(title);
        tbMessage.setContent(content);
        int i = messageService.saveMessage(tbMessage);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

    /**
     * 通过Mid查询message
     */
    @GetMapping("/message/one/{mid}")
    public ResultVO getMessageByMid(@PathVariable("mid") Integer mid){
        TbMessage tbMessage = messageService.getMessageByMid(mid);
        return tbMessage!=null?ResultVO.success("success",tbMessage):ResultVO.failure("failed");
    }

    /**
     * 修改消息
     */
    @PostMapping("/message/update")
    public ResultVO updateMessage(@RequestBody Map<String,Object> map){
        /**
         *  mid:this.messageUpdForm.mid,
         *         title: this.messageUpdForm.title,
         *         content: this.messageUpdForm.content
         */
        Integer mid = (Integer) map.get("mid");
        String title = (String) map.get("title");
        String content = (String) map.get("content");

        TbMessage tbMessage = new TbMessage();
        tbMessage.setMid(mid);
        tbMessage.setContent(content);
        tbMessage.setTitle(title);

        int i = messageService.updMessage(tbMessage);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

    /**
     * 禁用消息
     */
    @GetMapping("/message/softdel/{mid}")
    public ResultVO softdelMessage(@PathVariable("mid") Integer mid){
        TbMessage tbMessage = new TbMessage();
        tbMessage.setMid(mid);
        tbMessage.setSoftdel(0);
        int i = messageService.updMessage(tbMessage);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

    /**
     * 启用消息
     */
    @GetMapping("/message/use/{mid}")
    public ResultVO useMessage(@PathVariable("mid") Integer mid){
        TbMessage tbMessage = new TbMessage();
        tbMessage.setMid(mid);
        tbMessage.setSoftdel(1);
        int i = messageService.updMessage(tbMessage);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

}
